var tbodys=[]
var tb=document.querySelector("tbody")
var pageSize =5; //每页显示的记录条数
var page = 1; //当前页，默认第一页


//查看项目列表
function projectlist(){
    let list={
        pageindex:page,
        pagenum:pageSize
    }
    ajaxHttp("post","project/findProjectList",list).then((res)=>{
        res=JSON.parse(res)
        // console.log(res);
        var data=res.dataset.projectList
        tbodys=data
        for(var i=0;i<tbodys.length;i++){
            var tr=document.createElement("tr")
            tr.className="trs"
            tr.innerHTML=`
                <td>${tbodys[i].projectId}</td>
                <td>${tbodys[i].projectName}</td>
                <td>${tbodys[i].projectLeader}</td>
                <td>${tbodys[i].workingHours}</td>
                <td>${tbodys[i].startDate}</td>
                <td>${tbodys[i].endDate}</td>
                <td class="state">${tbodys[i].progress}%</td>
                <td class="operate">
                    <button onclick="students(${tbodys[i].projectId})" style="color:#2B6FDF;">详情</button>
                </td>
            `
            tb.appendChild(tr)
        }
    })
    // catch请求失败走的路线
}
projectlist()


console.log(tbodys);
// 搜索列表
function serch(){
}

// 添加项目接口
function addclass(){
}

// // 删除班级接口
// function delclass(){
//     var classid=7
//     var del={
//         clazzId:classid
//     }
//     console.log(this);
//     // ajaxHttp("post","clazz/selectClazz",del).then((res)=>{
//     //     console.log(res);
//     // }).catch((res)=>{
//     //     console.log("链接失败");
//     // })
// }

// 跳转项目详情
function students(rev){
    localStorage.setItem("projectId",rev)
    window.location.href="projectdetails.html"
}

// 修改班级
var trs=document.querySelectorAll(".trs")
console.log(trs);

// 添加项目列表
var addopen=document.querySelector(".addopen")
// 打开
function openadd(){
    addopen.style.display="flex"
}
// 关闭添加项目信息
function offopen(){
    addopen.style.display="none"
}



// 分页
var theUL = document.getElementById("pageListTableBody");
// console.log(theUL);
var totalPage = document.getElementById("spanTotalPage");
var pageNum = document.getElementById("spanPageNum"); //获取当前页<span>
var spanPre = document.getElementById("spanPre"); //获取上一页<span>
var spanNext = document.getElementById("spanNext"); //获取下一页<span>
var spanFirst = document.getElementById("spanFirst"); //获取第一页<span>
var spanLast = document.getElementById("spanLast"); //获取最后一页<span>
var numberRowsInTable = theUL.getElementsByTagName("tr").length; //记录总条数
console.log(numberRowsInTable);

//下一页
function next(){
    hideTable();
    currentRow = pageSize * page;
    maxRow = currentRow + pageSize;
    if ( maxRow > numberRowsInTable ) maxRow = numberRowsInTable;
    for ( var i = currentRow; i< maxRow; i++ ){
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    page++;
    if ( maxRow == numberRowsInTable ) {
        nextText();
        lastText();
    }
    showPage();
    preLink();
    firstLink();
}

//上一页
function pre(){
    hideTable();
    page--;
    currentRow = pageSize * page;
    maxRow = currentRow - pageSize;
    if ( currentRow > numberRowsInTable ) currentRow = numberRowsInTable;
    for ( var i = maxRow; i< currentRow; i++ ){
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    if ( maxRow == 0 ){
        preText();
        firstText();
    }
    showPage();
    nextLink();
    lastLink();
}

//第一页
function first(){
    hideTable();
    page = 1;
    for ( var i = 0; i<pageSize; i++ ){
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    showPage();
    firstText();
    preText();
    nextLink();
    lastLink();
}

//最后一页
function last(){
    hideTable();
    page = pageCount();
    currentRow = pageSize * (page - 1);
    for ( var i = currentRow; i<numberRowsInTable; i++ ){
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    showPage();
    preLink();
    nextText();
    firstLink();
    lastText();
}

function hideTable(){
    for ( var i = 0; i<numberRowsInTable; i++ ){
        theUL.getElementsByTagName("tr")[i].style.display = 'none';
    }
}

function showPage(){
    pageNum.innerHTML = page;
}

//总共页数
function pageCount(){
    return Math.ceil(numberRowsInTable/pageSize);
}
//显示链接
function preLink(){
    spanPre.innerHTML = "<a href='javascript:pre();'>上一页</a>";
}
function preText(){
    spanPre.innerHTML = "上一页";
}
function nextLink(){
    spanNext.innerHTML = "<a href='javascript:next();'>下一页</a>";
}
function nextText(){
    spanNext.innerHTML = "下一页";
}
function firstLink(){
    spanFirst.innerHTML = "<a href='javascript:first();'>首页</a>";
}
function firstText(){
    spanFirst.innerHTML = "首页";
}
function lastLink(){
    spanLast.innerHTML = "<a href='javascript:last();'>末页</a>";
}
function lastText(){
    spanLast.innerHTML = "末页";
}
//隐藏
function hide(){
    for ( var i = pageSize; i<numberRowsInTable; i++ ){
        theUL.getElementsByTagName("tr")[i].style.display = 'none';
    }
    totalPage.innerHTML = pageCount();
    pageNum.innerHTML = '1';
    nextLink();
    lastLink();
}
hide();